﻿using DoNetFrameworkWebApi.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

namespace DoNetFrameworkWebApi.Controllers
{
    public class UserController : ApiController
    {
        private IBaseRepository<User> _userRepository;
        public UserController()
        {
            this._userRepository = new BaseRepository<User>(new AppDbContext());
        }

        public UserController(IBaseRepository<User> userRepo)
        {
            _userRepository = userRepo;
        }

        public IEnumerable<User> GetUserList(string name, int sex)
        {
            var list = _userRepository.GetList();
            if (list?.Count() > 0)
                return list.Where(x => (string.IsNullOrEmpty(name) || x.Name.Contains(name)) &&
                (sex == 0 || x.Sex == sex));
            return null;
        }

        public User Get(int id)
        {
            return _userRepository.Get(id);
        }

        [HttpPost]
        public void Create(User user)
        {
            _userRepository.Insert(user);
        }

        [HttpPost]
        public void Delete(int id)
        {
            _userRepository.Delete(id);
        }

        [HttpPost]
        public void Update(User user)
        {
            _userRepository.Update(user);
        }
    }
}
